package com.example.studyPlatform.mapper;

import com.example.studyPlatform.bean.Comment;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CommentMapper {
    // 子查询优化分页
    @Select("select * from comments where postId = #{id} and id>=" +
            "(select id from comments where postId = #{id} limit #{offset},1) " +
            "limit #{rows}")
    List<Comment> getCommentsByPostId(Integer id, int offset, int rows);

    @Select("select * from comments where id = #{id}")
    Comment getCommentById(Integer id);

    @Insert("INSERT INTO comments (`authorUrl`,`email`,`content`,`postId`) VALUE (#{authorUrl},#{email},#{content},#{postId})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int publish(Comment comment);
}
